package deprecated;

import java.util.*;


/**
 * Constructs an xml frame.
 * The xml-tags contains the words which have been translated yet.
 * Words outside xml-tags have to be translated via statistical machine translation.
 *
 * @author Sabrina
 *
 */
public class MatchTarEasy {

       
        //Initialize Arrays: source, tmsource, target and matchedTarget
        private String[] source;
        private String[] tmsource;
        private String[] target;
        private boolean[] matchedTarget;
       
        private String[] insertion = new String[0];
       
        private String[] matchPathArr;
        
        
        
                       
        //TODO Put alignments of huge trainset in HashMap?
        //Contains alignments
        private Map<String, String> alignment = new HashMap<String, String>();
       
       

        public MatchTarEasy(String[] source, String[] tmsource, String[] tmtarget, Map<String,String> alignment){
        	
        	this.source = source;
        	this.target = tmtarget;
        	this.tmsource = tmsource;
        	this.alignment = alignment;
        	
        	//length correlates with the length of longer input
        	DistanceSetArray matchPath = new DistanceSetArray(this.source, this.tmsource);
        	ArrayList<String> dummy = matchPath.getBacktrace();
        	this.matchPathArr = new String[dummy.size()];
        	for(int i=0; i<dummy.size();i++){
        		this.matchPathArr[i] = dummy.get(i);
        	}
        	
        	insertion = new String[matchPathArr.length];
              	
        }
        
        
        /**
         * Finds the mismatched area and gives it to the mismatch-function
         * @return Returns the return-value of the method constructXml as String
         * source: to be translated
         * tmsource: FuzzyMatch out of translation memory
         * target: translation of tmsource
         */
        public String findMismatch(){
               
        		System.out.println(Arrays.toString(this.matchPathArr));
        		System.out.println("source: "+Arrays.toString(this.source));
        		System.out.println("tmSource: "+Arrays.toString(this.tmsource));
        		System.out.println("target: "+Arrays.toString(this.target));
               
                //declaration and initialization of index-variables
                int posI=0;
                int posS=0;
               
                int startI = 0;
                int startS = 0;
               
                String edit = "";
               
                boolean matching = false;
               
                //Necessary for the method construct-xml
                
                //TODO
                //length of matchedTarget equals the longer data???
                
                //
                //int maxDataLength = Math.max(source.length, target.length);
                
                matchedTarget = new boolean[matchPathArr.length];
                for (int i=0; i<matchedTarget.length; i++){
                        matchedTarget[i] = true;
                }
               
                //Comparing source and tmsource
                for(int i = 0; i<this.matchPathArr.length-1; i++){
                	
                       
                        //Set edit to Match / Replacement / Insertion / Deletion
                        edit = this.matchPathArr[i];
                        
                        //BEGINNING OF MISMATCH
                        if(matching && edit != " Match "){
                                startI = posI;
                                startS = posS;
                                matching = false;
                        }
                       
                        //END OF MISMATCH
                        else if(!matching && edit == " Match "){
                        	//System.out.println("startI: "+startI + "posI: "+ posI +"startS: "+startS+"posS: "+posS);
                                mismatch(startI, posI-1, startS, posS-1);
                                matching = true;
                        }
                       
                        if(!edit.equals(" Deletion ")){
                                posI++;
                        }
                               
                        if(!edit.equals(" Insertion ")){
                                posS++;
                        }
               
              //  System.out.println("startI: "+startI+" posI: "+posI);
                }
                               
                if(!matching){
                        mismatch(startI, posI, startS, posS);
                }
               
                return constructXml();
               
        }

        /**
         * Finds the insertion position for source-words that aren´t in tmSource, no return value
         * @param startI: is start-index of mismatch in the source
         * @param endI: is end-index of mismatch in the source
         * @param startS: is start-index of mismatch in the tmSource
         * @param endS:is end-index of mismatch in the tmSource
         */
        private void mismatch(int startI, int endI, int startS, int endS) {
       
                //sets matchedTarget for mismatchTmSource to false
                //remove use of affected target words
                
               for (int i=0; i<matchPathArr.length; i++){
            	   if(!matchPathArr[i].equals(" Match ")){
            		   matchedTarget[i] = false;
            	   }
               }
               System.out.println(Arrays.toString(matchedTarget));
               
               
                //Set insT to minimum targetPosition

               		
            			//not for deletions because they won´t be included in xml
                        for (int i = 0; i<matchedTarget.length; i++){
                        	if(matchedTarget[i] == false && matchPathArr[i]!= " Deletion "){
                                insertion[i] = this.source[i];
                        	}
                        }
	                  
        }
       
        /**
         * Constructs a xml via given data (matchedTarget, insertion)
         *
         * @return a xml-construct as String
         * containing the translated words in xml-tags and the residual words outside
         * residual words must be translated via statistical machine translation
         */
        private String constructXml() {
        	
        	int numberOfMismatchAreas = 0;
            for (int i=0; i<this.matchedTarget.length; i++){
            	if(i==this.matchedTarget.length-1 && this.matchedTarget[i] == false){
            		numberOfMismatchAreas++;
            	}
            	else if(this.matchedTarget[i] == false && this.matchedTarget[i+1] == true){
            		numberOfMismatchAreas++;
            	}
            }
            System.out.println("NumbOfMisAreas: "+numberOfMismatchAreas);
               
                String xml = "";
                boolean included = false;
                int startT = -1;
               
           while(numberOfMismatchAreas > 0){
                for (int t=0; t<target.length; t++){
                	if(!included && matchedTarget[t]){
                		startT = -1;
                		included = true;
                	}
                	else if(included && (!matchedTarget[t] || insertion[t]!=null)){
                		startT ++;
                		if(startT >= 0){
                			xml += "<xml translation =";
                			for(int i = startT; i<t; i++){
                				xml += " "+target[i]+" ";
                				xml += "> </xml>";
                				numberOfMismatchAreas --;
                			}
                		}
                		included = false;
                	}
                	if(insertion[t]!=null){
                		xml += " "+insertion[t]+" ";
                		
                	}
                	
                }
           }
        return xml;
        }
               
    

       
        /**
         * @param args
         */
        public static void main(String[] args) {
                //TODO TESTING!
        		//TODO VERMUTUNG: PROBLEM BEI START I / END I
               
                //[ Match ,  Match ,  Replacement ,  Replacement ,  Replacement ,  Insertion ,  Match ]
               
           /*   MatchTar.source = "das ist der kurze dumme satz".split(" ");
                MatchTar.tmsource = "das ist ein komplizierterer zweiter dummer satz".split(" ");
                MatchTar.target = new String[]{"this", "is", "a", "more complicated",
                                "second", "stupid", "sentence"}; */
               
                //Fills the alignment HashMap
            /*  alignment.put("das", "this"); alignment.put("ist", "is"); alignment.put("der", "the");
                alignment.put("kurze", "short"); alignment.put("dumme", "stupid"); alignment.put("satz", "sentence");
                alignment.put("ein", "a"); alignment.put("komplizierterer", "more complicated");
                alignment.put("zweiter", "second"); alignment.put("dummer", "stupid"); */
       
                String[] s= "der ast und zweig ist grün".split(" ");
                String[] tms = "der stamm ist grün und riecht".split(" ");
                String[] tmt = "the trunk is green and smells".split(" ");
                Map<String,String> alignment = new HashMap<String,String>();
                alignment.put("der", "the"); alignment.put("stamm", "trunk"); alignment.put("ist", "is");
                alignment.put("grün", "green"); alignment.put("und", "and"); alignment.put("riecht", "smells");
                
               //Ausgabe:  <xml translation= the > </xml> ast <xml translation= is green and > </xml> stinkt  sehr JUCHU!
                
                MatchTarEasy set1 = new MatchTarEasy(s,tms,tmt,alignment);
               
                System.out.println(set1.findMismatch());
               
                //FUNKTIONIERT (gleiche Satzlänge, nur eine Veränderung)
        /*      MatchTar.source = "der ast ist grün".split(" ");
                MatchTar.tmsource = "der stamm ist grün".split(" ");
                MatchTar.target = "the trunk is green".split(" ");
                alignment.put("der", "the"); alignment.put("stamm", "trunk"); alignment.put("ist", "is");
                alignment.put("grün", "green");
               
                //ZIEL: <xml translation = "the" > "ast" <xml translation = "is green"></xml>
         */
               
               

        }

}